#!/usr/bin/env python
#
#  EventMonitor.py
#
#  Copyright (C) 2008 Johnny Jacob <johnnyjacob@gmail.com>
#
#  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
#
#  Permission is hereby granted, free of charge, to any person obtaining a
#  copy of this software and associated documentation files (the "Software"),  
#  to deal in the Software without restriction, including without limitation  
#  the rights to use, copy, modify, merge, publish, distribute, sublicense,  
#  and/or sell copies of the Software, and to permit persons to whom the  
#  Software is furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in 
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
#  DEALINGS IN THE SOFTWARE.
#

import wnck
import gobject
import datetime

from . import Logger


# A Event object has a timer. The monitor once it recieves a event
# It will start the timer and hook on to a signal (?). When the monitor
# recieves the same event, the event object will be destroyed. On
# destroy it will commit itself to the logger queue.

# Kinds of event should inherit from here . must call base record.

class Event:
    def __init__():
        self.startTime = datetime.datetime.now ()
        self.logger = None

    def __init__(self):
        self.startTime = datetime.datetime.now ()
        self.logger = None

    def record(self, screen, arg3, window):
        screen.disconnect_by_func (self.record)
        self.appName = window.get_application().get_name()
        self.appTitle = window.get_name()
        self.endTime = datetime.datetime.now()
        self.duration = self.endTime - self.startTime
        self.logger.log (self)
        

class EventMonitor:
    def __init__():
        self.logger = Logger.Logger()

    def __init__(self):
        self.logger = Logger.Logger()

    def event_active_window (self, screen, p_active_window, data=None):
        screen.force_update()
        for w in  screen.get_windows() :
            if w.is_active() :
                this_event = Event ()
                this_event.logger = self.logger
                screen.connect ("active_window_changed", this_event.record, w)

    def Start (self):
        screen = wnck.screen_get_default()
        screen.force_update()
        screen.connect ("active_window_changed", self.event_active_window, None)

    def main (self):
        self.Start()

if __name__ == "__main__":
    eventmonitor = EventMonitor()
    eventmonitor.main()
